package com.shb.game;

import java.util.Random;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class PicNumActivity extends BaseActivity {
	private GridView gridMain;
	private Button btnBegin;
	private Button btnLeft;
	private Button btnUp;
	private Button btnRight;
	private Button btnDown;
	private Spinner spSelect;
	private Button btnHelp;
	private TextView tvNum;
	/** 适配器 */
	private BaseAdapter adapter;
	/** 行列 */
	private int row = 4;
	private int col = 4;
	/** 目标数组 */
	private int[] desInt = new int[0];
	/** 选关 */
	private final String[] select = new String[] { "4＊4（简单）", "5＊5（一般）", "5＊6（普通）", "6＊6（较难）", "6＊7（困难）", };

	private static int clicknum = 0;

	private final int[] colArray = new int[] { 4, 5, 5, 6, 6 };
	private final int[] rowArray = new int[] { 4, 5, 6, 6, 7 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pic_num);
		gridMain = (GridView) findViewById(R.id.gridMain);
		btnLeft = (Button) findViewById(R.id.btnLeft);
		btnUp = (Button) findViewById(R.id.btnUp);
		btnRight = (Button) findViewById(R.id.btnRight);
		btnDown = (Button) findViewById(R.id.btnDown);
		btnHelp = (Button) findViewById(R.id.btnHelp);
		btnBegin = (Button) findViewById(R.id.btnBegin);
		spSelect = (Spinner) findViewById(R.id.spSelect);
		tvNum = (TextView) findViewById(R.id.tvNum);
		btnLeft.setOnClickListener(this);
		btnUp.setOnClickListener(this);
		btnRight.setOnClickListener(this);
		btnDown.setOnClickListener(this);
		btnHelp.setOnClickListener(this);
		btnBegin.setOnClickListener(this);
		adapter = new BaseAdapter() {

			@Override
			public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
				TextView btn = new TextView(PicNumActivity.this);
				btn.setHeight(40);
				if (desInt[paramInt] != 0) {
					btn.setText(desInt[paramInt] + "");
					btn.setBackgroundResource(R.drawable.btn_bg);
				} else {
					btn.setText(" ");
				}
				btn.setTextColor(Color.BLACK);
				btn.setGravity(Gravity.CENTER);
				return btn;
			}

			@Override
			public long getItemId(int paramInt) {
				return paramInt;
			}

			@Override
			public Object getItem(int paramInt) {
				return paramInt;
			}

			@Override
			public int getCount() {
				return desInt.length;
			}
		};
		gridMain.setAdapter(adapter);
		spSelect.setAdapter(new ArrayAdapter(this, android.R.layout.simple_spinner_item, select));
		spSelect.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
				row = rowArray[paramInt];
				col = colArray[paramInt];
				initDate();
			}

			@Override
			public void onNothingSelected(AdapterView<?> paramAdapterView) {
			}

		});
		initDate();
	}

	// 移动
	private void go(int s, boolean isCheck) {
		int r = 0, c = 0;
		boolean bIsChange = false;
		// 1上，2 下，3 左，4右
		for (int i = 0; i < desInt.length; i++) {
			if (desInt[i] == 0) {
				r = i / col;
				c = i % col;
				break;
			}
		}
		if (s == 1) {
			if (r < row - 1) {
				change(r * col + c, (r + 1) * col + c);
				bIsChange = true;
			}
		} else if (s == 2) {
			if (r > 0) {
				change(r * col + c, (r - 1) * col + c);
				bIsChange = true;
			}
		} else if (s == 4) {
			if (c > 0) {
				change(r * col + c, r * col + c - 1);
				bIsChange = true;
			}
		} else if (s == 3) {
			if (c < col - 1) {
				change(r * col + c, r * col + c + 1);
				bIsChange = true;
			}
		}
		adapter.notifyDataSetChanged();
		if (isCheck && bIsChange) {
			clicknum++;
			tvNum.setText(clicknum + "");
			check();
		}
	}

	private void check() {
		for (int i = 0; i < row * col - 1; i++) {
			if (desInt[i] != i + 1) {
				return;
			}
		}
		Toast.makeText(this, "恭喜你用了" + clicknum + "次移动完成了！", 0).show();
		initDate();
	}

	/** 初始化数据 */
	private void initDate() {
		gridMain.setNumColumns(col);
		clicknum = 0;
		tvNum.setText(clicknum + "");
		desInt = new int[row * col];
		for (int i = 0; i < row * col - 1; i++) {
			desInt[i] = i + 1;
		}
		int n = 2001;
		while (n > 0) {
			int k = new Random().nextInt(4);
			go(k + 1, false);
			n--;
		}
	}

	/** 交换 */
	private void change(int a1, int a2) {
		int t = desInt[a1];
		desInt[a1] = desInt[a2];
		desInt[a2] = t;
	}

	@Override
	public void onClick(View paramView) {
		switch (paramView.getId()) {
		case R.id.btnLeft:
			go(3, true);
			break;
		case R.id.btnUp:
			go(1, true);
			break;
		case R.id.btnRight:
			go(4, true);
			break;
		case R.id.btnDown:
			go(2, true);
			break;
		case R.id.btnHelp:
			startActivity(new Intent(this, PicNumHelpActivity.class));
			break;
		case R.id.btnBegin:
			initDate();
			break;
		}
	}
}
